//该模块处理用户评论的信息
const express = require("express");
const router = express.Router();
const url = require("url");
const fs = require("fs");

const {
  getCommentListServer,
  addUserCommentServer,
  getUserImgList,
  addOtherCommentServer,
  getOtherCommentListServer,
  getAdminIsNoComListServer,
  getNoComListServer,
  deleteOneReplyServer,
} = require("../service/newMessageService");
//获取管理员信息
// let adminInfo = {}
// admin.getAdminInfo(function(data){
//   adminInfo = data
// })
//渲染层主的评论列表
router.get("/getCommentList", async (req, res) => {
  //获取需要加载的文章id
  let parseurl = url.parse(req.url, true);
  let obj = parseurl.query;
  let articleId = obj.articleId;
  //从数据库中查找评论列表
  res.send(await getCommentListServer(articleId));
});

//评论时获取头像列表数据
router.get("/getUserImgList", async (req, res) => {
  const result = await getUserImgList();
  res.send(result);
});

//定义层主级别的回复
router.post("/replyByMain", async (req, res) => {
  res.send(await addUserCommentServer(req.body));
});

// 定义一个楼层中其他用户的评论(管理员回复)
router.post("/replyByMainor", async (req, res) => {
  res.send(await addOtherCommentServer(req.body));
});

//渲染其他用户的评论列表
router.get("/getOtherCommentList", async (req, res) => {
  //获取需要加载的楼层id
  let parseurl = url.parse(req.url, true);
  let obj = parseurl.query;
  let ceId = obj.ceId;
  res.send(await getOtherCommentListServer(ceId));
});
//获取含有管理员未回复的文章列表
router.get("/getAdminIsNoComList", async (req, res) => {
  res.send(await getAdminIsNoComListServer());
});

//根据文章id获取未回复的评论信息
router.get("/getNoComList", async (req, res) => {
  let articleId = req.query.id;
  res.send(await getNoComListServer(articleId));
});
router.get("/delCommentReply", async (req, res) => {
  console.log(`output->`, req.query);
  let articleId = req.query.articleId;
  let ceId = req.query.ceId;
  let id = req.query.id;
  res.send(await deleteOneReplyServer(articleId,ceId,id));
});
module.exports = router;
